﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace GONES.Tools.Common
{
    /// <summary>
    /// 随机数工具类
    /// </summary>
    public static class Rnd
    {
        /// <summary>
        /// 系统GUID
        /// </summary>
        public static string Guid
        {
            get { return System.Guid.NewGuid().ToString().Replace("-", "").ToLower(); }
        }

        /// <summary>
        /// 数字随机数
        /// </summary>
        /// <param name="len">生成长度</param>
        /// <returns>数字随机数</returns>
        public static string RandNum(int len)
        {
            var num = new StringBuilder();

            var rnd = new Random();

            for (int i = 0; i < len; i++)
            {
                int number = rnd.Next();
                num.Append((char) ('0' + (char) (number%10)));
            }

            return num.ToString();
        }

        /// <summary>
        /// 字母随机数
        /// </summary>
        /// <param name="len">生成长度</param>
        /// <returns>字母随机数</returns>
        public static string RandLetter(int len)
        {
            var letter = new StringBuilder();

            var rnd = new Random();

            for (int i = 0; i < len; i++)
            {
                int number = rnd.Next();
                letter.Append((char) ('A' + (char) (number%26)));
            }

            return letter.ToString();
        }

        /// <summary>
        /// 数字和字母随机数
        /// </summary>
        /// <param name="len">生成长度</param>
        /// <returns>数字和字母随机数</returns>
        public static string RandCode(int len)
        {
            var code = new StringBuilder();

            var rnd = new Random();

            for (int i = 0; i < len; i++)
            {
                int number = rnd.Next();

                if (number%2 == 0)
                {
                    code.Append((char) ('0' + (char) (number%10)));
                }
                else
                {
                    code.Append((char) ('A' + (char) (number%26)));
                }
            }

            return code.ToString();
        }

        /// <summary>
        /// SHA256函数
        /// </summary>
        /// /// <param name="str">原始字符串</param>
        /// <returns>SHA256结果</returns>
        public static string SHA256(string str)
        {
            byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
            var Sha256 = new SHA256Managed();
            byte[] Result = Sha256.ComputeHash(SHA256Data);
            return Convert.ToBase64String(Result); //返回长度为44字节的字符串
        }

        /// <summary>
        /// MD5函数
        /// </summary>
        /// <param name="str">原始字符串</param>
        /// <returns>MD5结果</returns>
        public static string MD5(string str)
        {
            byte[] b = Encoding.Default.GetBytes(str);
            b = new MD5CryptoServiceProvider().ComputeHash(b);
            string ret = "";
            for (int i = 0; i < b.Length; i++)
            {
                ret += b[i].ToString("x").PadLeft(2, '0');
            }
            return ret;
        }
    }
}